<?php
namespace Home\Controller;
use Think\Controller;
header('content-type:text/html;charset=utf-8');
class IndexController extends BaseController
{
    public function index(){
        //按降序查处所有的电影
        $id=session('Home.id');
        $movies=M('movie')->order("created_at desc")->select();
        array_push($movies,['name'=>'请选择']);
        $room=M('room')->where(['user_id'=>$id])->select();
        array_push($room,['name'=>'请选择']);
        $user=M('user')->where(['id'=>$id])->select();
        $model=D('Screening');
        $count=count($model->setAll($id));
        $Page= new \Think\Page($count,5);
        $Page->setConfig('prev',上一页);
        $Page->setConfig('next',下一页);
        $show = $Page->show();
        $limt=$Page->firstRow.','.$Page->listRows;
        $season=$model->setAll($id,$limt);
        $this->assign('username',session('Home.username'));
        $this->assign('season',$season);
        $this->assign('page',$show);
        $this->assign('movie',$user[0]['name']);
        $this->assign('movies',$movies);
        $this->assign('room',$room);
        $this->display();
    }
    //删除图片
    public function setunlink($filename){
        $path='./public/upload/';
        unlink($path.$this->substr($filename).$filename);
    }

       public function add(){
       $model=D('Screening');
                if($_POST['name']==''){
                       session('ts','请选择电影');
            $this->redirect('Index/index');
                  }

               if($_POST['fyt']==''){
                       session('ts','请选择放映厅');
            $this->redirect('Index/index');
                  }
        
        if(empty($_POST['price'])){
            session('ts','价格不能为空');
            $this->redirect('Index/index');
        }

        if(!is_numeric ($_POST['price'])){
            session('ts','请输入正确价格');
            $this->redirect('Index/index');
        }else if(strlen($_POST['price'])>8){
                      session('ts','你丫的电影票卖那么贵?');
            $this->redirect('Index/index');
                 }
        $data['user_id']=session('Home.id');
        $data['movie_id']=$_POST['name'];
        $data['room_id']=$_POST['fyt'];
        $time=$_POST['timey'].'-'.$_POST['timem'].'-'.$_POST['timed'].' '.$_POST['timeh'].':'.$_POST['timei'];
           $data['showtime']=strtotime($time);
           if(strtotime($time)-time()<0){
               session('ts','放映时间不能小于当前时间!');
               $this->redirect('Index/index');
           }
      $data['price']=$_POST['price'];
        $result=$model->getAll($data);  
       if($result){
            session('ts','添加成功');
            $this->redirect('Index/index');
        }
    }

    public function create(){
        $data=$_GET;
        $id=session('Home.id');
        $movies=M('movie')->order("created_at desc")->select();
        $model=M('screening')->where(['id'=>$_GET['id']])->select();
        $stime=$model[0]['showtime'];
        if(date('m',$stime)<11){
            $m=substr(date('m',$stime),1,2);
        }else{
            $m=date('m',$stime);
        }
        if(date('d',$stime)<11){
            $d=substr(date('d',$stime),1,2);
        }else{
            $d=date('d',$stime);
        }
        if(date('H',$stime)<11){
            $h=substr(date('H',$stime),1,2);
        }else{
            $h=date('H',$stime);
        }
        if(date('i',$stime)<11){
            $mi=substr(date('i',$stime),1,2);
        }else{
            $mi=date('i',$stime);
        }
        $this->assign('m',$m);
        $this->assign('d',$d);
        $this->assign('h',$h);
        $this->assign('mi',$mi);
        $movie=$model[0]['movie_id'];
        $room=$model[0]['room_id'];
        $this->assign('stime',$stime);
        $this->assign('movie',$movie);
        $this->assign('r',$room);
        $room=M('room')->where(['user_id'=>$id])->select();
        $this->assign('movies',$movies);
        $this->assign('room',$room);
        $this->assign('data',$data);
        $this->display();
    }

    public function update(){
        $id=$_POST['id'];
        $_POST['showtime']=strtotime($_POST['showtime']);
       if(empty($_POST['price'])){
            echo 3;
            return false;
        }
        $model=M('Screening')->where(['id'=>$id])->save($_POST);
        if($model){
            echo 1;
        }else{
            echo 2;
        }
    }
}